﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestScript
{
    class TestAim
    {

        /*
         * 1. Create obj1 at cam position & constantly update position & heading
         * 2. Create obj2 attached to obj1 by offset
         * 3. Bingo, obj2 coords is where we are aiming at.
         * */

        GTA.GameObject CamObj;
        GTA.GameObject AimObj;

        enum eModes
        {
            Idle,
            Attaching,
            Active
        }
        eModes Mode = eModes.Idle;

        public TestAim()
        {
            GTA.Events.RunTick.Handler += RunTick;
        }

        public void AttachAim()
        {
            Mode = eModes.Attaching;
            Log.Debug("Attachaim");
            GTA.Player pl = new GTA.Player();
            GTA.Vector3 pos = pl.Coordinates;
            GTA.ScriptApi.RequestModel(GTA.eModel.MODEL_CJ_ARROW);
            GTA.ScriptApi.RequestModel(GTA.eModel.MODEL_CJ_BARRIER_1);
            CamObj = new GTA.GameObject();
            AimObj = new GTA.GameObject();
            GTA.ScriptApi.CreateObject(GTA.eModel.MODEL_CJ_ARROW, pos.X, pos.Y, pos.Z, CamObj, true);
            GTA.ScriptApi.CreateObject(GTA.eModel.MODEL_CJ_BARRIER_1, pos.X, pos.Y, pos.Z, AimObj, true);
            Log.Debug("Attachaim2");
            if (!GTA.GameObject.Exists(CamObj)) return;
            if (!GTA.GameObject.Exists(AimObj)) return;
            Log.Debug("Attachaim3");
            GTA.ScriptApi.SetObjectVisible(CamObj, false);
            GTA.ScriptApi.SetObjectCollision(CamObj, false);
            GTA.ScriptApi.SetObjectCollision(AimObj, false);

            GTA.ScriptApi.MarkModelAsNoLongerNeeded(GTA.eModel.MODEL_CJ_ARROW);
            GTA.ScriptApi.MarkModelAsNoLongerNeeded(GTA.eModel.MODEL_CJ_BARRIER_1);

            GTA.ScriptApi.AttachObjectToObject(AimObj, CamObj, 0, 0.0F, 40.0F, 0.0F, 0.0F, 0.0F, 0.0F);
            Mode = eModes.Active;
            Log.Debug("Attachaim4");
        }

        public void DetachAim()
        {
            if (GTA.GameObject.Exists(CamObj)) GTA.ScriptApi.DeleteObject(CamObj);
            if (GTA.GameObject.Exists(AimObj)) GTA.ScriptApi.DeleteObject(AimObj);
            Mode = eModes.Idle;
        }

        unsafe public void RunTick(Object sender, EventArgs args)
        {
            if (Mode == eModes.Idle) return;
            if (Mode == eModes.Attaching) { AttachAim(); return; }
            Log.Debug("TestAim tick");
            GTA.Camera cam = new GTA.Camera();
            GTA.ScriptApi.GetGameCam(cam);
            //GTA.Vector3 campos = new GTA.Vector3();
            float camposX, camposY, camposZ;
            GTA.ScriptApi.GetCamPos(cam, &camposX, &camposY, &camposZ);
            //GTA.Vector3 camdir = new GTA.Vector3();
            float camdirX, camdirY, camdirZ;
            GTA.ScriptApi.GetCamRot(cam, &camdirX, &camdirY, &camdirZ);
            if (GTA.GameObject.Exists(CamObj))
            {
                Log.Debug("TestAim tick moving obj");
                CamObj.Coordinates = new GTA.Vector3(camposX,camposY,camposZ);
                GTA.ScriptApi.SetObjectRotation(CamObj, camdirX, camdirY, camdirZ);
            }

            //if (GTA.GameObject.Exists(AimObj))
            //{

            //}
        }
    }
}
